package cdu.sl.service.impl;

import cdu.sl.custom.CustomUserDetails;
import cdu.sl.pojo.dto.StudentGeneralReviewPageQueryDTO;
import cdu.sl.pojo.vo.StudentGeneralReviewVO;
import cdu.sl.repository.basic.StudentRepository;
import cdu.sl.result.PageResult;
import cdu.sl.service.StudentService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
@RequiredArgsConstructor
public class StudentServiceImpl implements StudentService {

    private final StudentRepository studentRepository;


    /**
     * 根据角色分页条件查询学生成绩数据
     *
     * @param studentGeneralReviewPageQueryDTO 查询参数
     */
    @Override
    public PageResult<StudentGeneralReviewVO> page(StudentGeneralReviewPageQueryDTO studentGeneralReviewPageQueryDTO) {
        // 先判断角色 根据角色返回不同的数据
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        CustomUserDetails customUserDetails = (CustomUserDetails) authentication.getPrincipal();
        return doPage(studentGeneralReviewPageQueryDTO, customUserDetails);
    }


    private PageResult<StudentGeneralReviewVO> doPage(StudentGeneralReviewPageQueryDTO studentGeneralReviewPageQueryDTO, CustomUserDetails customUserDetails) {
        String roleName = customUserDetails.getUser().getRoles().get(0).getName();
        Long schoolId = customUserDetails.getUser().getSchoolId();
        Long classId = customUserDetails.getUser().getClassId();
        if (roleName.equals("HEAD_TEACHER")) {
            log.info("班主任分页条件查询学生数据");
            return doPageHeadTeacher(studentGeneralReviewPageQueryDTO, classId);
        } else if (roleName.equals("SCHOOL_ADMINISTRATOR")) {
            log.info("校管理员分页查询学生数据");
            return doPageSchoolAdmin(studentGeneralReviewPageQueryDTO, schoolId);
        } else
            return null;
    }

    /**
     * 校级管理员需要的学生数据
     *
     * @param queryDTO 查询与分页条件
     */
    private PageResult<StudentGeneralReviewVO> doPageSchoolAdmin(StudentGeneralReviewPageQueryDTO queryDTO, Long schoolId) {
        Pageable pageable = PageRequest.of(queryDTO.getPage() - 1, queryDTO.getPageSize());
        Integer currentYear = LocalDateTime.now().getYear();
        Page<StudentGeneralReviewVO> voPage = studentRepository.findStudentGeneralReviewsForAdmin(schoolId, queryDTO.getStage(), queryDTO.getGrade(), queryDTO.getClassId(), currentYear, pageable);
        log.info("学校管理员查询到学生数据：{}", voPage.getTotalElements());
        return new PageResult<>(voPage.getTotalElements(), voPage.getContent(),
                voPage.getNumber() + 1, voPage.getSize(), voPage.getTotalPages());

    }


    /**
     * 查询班主任视角所需学生数据
     *
     * @param queryDTO 查询与分页条件
     */
    private PageResult<StudentGeneralReviewVO> doPageHeadTeacher(StudentGeneralReviewPageQueryDTO queryDTO, Long classId) {
        Pageable pageable = PageRequest.of(queryDTO.getPage() - 1, queryDTO.getPageSize());
        Integer currentYear = LocalDateTime.now().getYear();
        Page<StudentGeneralReviewVO> voPage = studentRepository.findStudentGeneralReviewsForTeacher(classId, currentYear, pageable);
        log.info("班主任查询到学生数据：{}", voPage.getTotalElements());
        return new PageResult<>(voPage.getTotalElements(), voPage.getContent(),
                voPage.getNumber() + 1, voPage.getSize(), voPage.getTotalPages());
    }


}
